package com.spring.example.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;

/**
 * @author zhouhengzhe
 * @description:
 * @date 2021/11/30 下午8:56
 * @since v1
 */
@Configuration
@ComponentScan("com.spring.example")
@MapperScan("com.spring.example.mapper")
public class AppConfig {


	@Bean
	public DataSource dataSource(){
		DriverManagerDataSource driverManagerDataSource
				= new DriverManagerDataSource();
		driverManagerDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
		driverManagerDataSource.setPassword("food_mall");
		driverManagerDataSource.setUsername("food_mall");
		driverManagerDataSource.setUrl("jdbc:mysql://106.52.205.232:3306/food_mall?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC");
		return driverManagerDataSource;
	}

	@Bean
	public SqlSessionFactory sqlSessionFactory() throws Exception {
		SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
		factoryBean.setDataSource(dataSource());
		return factoryBean.getObject();
	}


//	@Bean
//	public UserMapper userMapper(){
//		NxSession nxSession = new NxSession();
//		UserMapper mapper = (UserMapper) nxSession.getProxyMapper(UserMapper.class);
//		return mapper;
//	}

}
